// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:multi_shop/models/user.dart';
import 'package:multi_shop/services/user_api.dart';

import '../widgets/order_status_bar.dart' show OrderStatusBar;

class My extends StatefulWidget {
  const My({super.key});

  @override
  State<My> createState() => _MyState();
}

class _MyState extends State<My> {
  final _api = UserApi();
  final storege = GetStorage();
  UserInfo? userInfo;
  @override
  void initState() {
    super.initState();
    _getUserInfo();
  }

  Future<void> _getUserInfo() async {
    var data = await _api.getUserInfo();
    setState(() {
      userInfo = data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('我的')),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  InkWell(
                    onTap: () {
                      Get.toNamed('/profile');
                    },
                    splashColor: Colors.transparent, // 波纹颜色
                    highlightColor: Colors.transparent, // 高亮颜色
                    hoverColor: Colors.transparent,
                    child: Container(
                      child: Row(
                        children: [
                          Container(
                            margin: EdgeInsets.only(right: 10),
                            width: 50,
                            height: 50,
                            child: CircleAvatar(
                              backgroundImage:
                                  userInfo?.avatar.isNotEmpty == true
                                  ? NetworkImage(userInfo!.avatar)
                                  : AssetImage('assets/images/avatar.jpeg'),
                            ),
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(left: 3.0),
                                child: Text(
                                  userInfo?.username ?? '',
                                  style: TextStyle(fontSize: 16),
                                ),
                              ),
                              GestureDetector(
                                onTap: () {
                                  Get.toNamed('/address');
                                },
                                child: Row(
                                  children: [
                                    Icon(
                                      Icons.location_on,
                                      size: 16.0,
                                      color: Colors.grey,
                                    ),
                                    Text(
                                      '收货地址',
                                      style: TextStyle(color: Colors.grey),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(height: 10),
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        children: [
                          GestureDetector(
                            onTap: () {
                              Get.toNamed('/order_list');
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text('我的订单'),
                                Icon(Icons.arrow_forward_ios, size: 16),
                              ],
                            ),
                          ),
                          SizedBox(height: 10),
                          const OrderStatusBar(),
                          const SizedBox(height: 20),
                          SizedBox(height: 10),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(height: 10),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
